package classtrain;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @DESCRIPATION
 * @AUTHOR 戴超
 * @DATE 2018/6/19
 **/
public class SendImagSocketTest {
    public static void main(String[] args) throws Exception {
        ServerSocket server = null; //  声明ServerSocket 对象
        Socket client = null; //  一个Socket 对象表示一个客户端
        server = new ServerSocket(8888);// 服务器在8888 端口上等待客户端访问
        System.out.println(" 服务器运行, 等待客户端连接");
        client = server.accept(); //  程序再此阻塞, 等待客户端连接
        System.out.println("连接成功");
        BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("image.jpg"));
        byte[] bytes = new byte[1024];
        OutputStream out = client.getOutputStream();
        int index = 0;
        while ((index = inputStream.available()) >0) {
            inputStream.read(bytes);
            out.write(bytes); //  输出信息
            out.flush();
        }
        out.close(); //  关闭打印流
        client.close(); //  关闭客户端连接
        server.close(); //
        inputStream.close();
    }

}